<div class="layui-fluid layui-anim febs-anim" id="febs-shellSite" lay-title="壳牌油站管理">
    <div class="layui-row febs-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" lay-filter="shellSite-table-form" id="shellSite-search-form">
                        <div class="layui-row">
                            <div class="layui-col-md9">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">油站编码</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="s-siteCode" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">油站名称</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="s-siteName" id="s-siteName" class="layui-input">
                                        </div>
                                    </div>
                                   
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">油站区域</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="s-siteArea" id="s-siteArea" lay-filter="s-siteArea"
	                       class="layui-input"> 
                                        </div>
                                    </div>
                                    
                                </div>
                            </div>
                            <div class="layui-col-md3 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="sitequery">
                                    <i class="layui-icon">&#xe848;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="sitereset">
                                    <i class="layui-icon">&#xe79b;</i>
                                </div>
                                <div shiro:hasPermission="shell:add"  class="layui-btn layui-btn-sm layui-btn-primary table-action" id="siteadd">
                                    <i class="layui-icon">&#xe624;</i>
                                </div>
                               
                                <div shiro:hasPermission="shell:delete"  class="layui-btn layui-btn-sm layui-btn-primary table-action" id="sitedelete">
                                    <i class="layui-icon">&#xe640;</i>
                                </div>
                                
                                 <div shiro:hasPermission="shell:export" class="layui-btn layui-btn-sm layui-btn-primary table-action" id="siteexport">
                                    <i class="layui-icon">&#xe67d;</i>
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="shellSiteTable" lay-data="{id: 'shellSiteTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="shellSite-titlink">
    <a th:onclick="viewSiteInfo('{{ d.siteid }}','{{ d.siteName }}')">{{ d.siteName }}</a>
</script>
<script type="text/html" id="shellSite-option">
    <span shiro:lacksPermission="shell:view,shell:update,shell:delete">
        <span class="layui-badge-dot febs-bg-orange"></span> 无权限
    </span>
	<span shiro:hasPermission="shell:info"><button lay-event="infoadd" type="button" class="layui-btn layui-btn-xs">其它信息</button></span>
	<span shiro:hasPermission="shell:update"><button lay-event="update" type="button" class="layui-btn layui-btn-xs">修改</button></span>
	<span shiro:hasPermission="shell:delete"><button lay-event="delete" type="button" class="layui-btn layui-btn-xs">删除</button></span>
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdown', 'jquery', 'laydate', 'form', 'table', 'febs', 'treeSelect'], function () {
        var $ = layui.jquery,
            laydate = layui.laydate,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            treeSelect = layui.treeSelect,
            dropdown = layui.dropdown,
            $view = $('#febs-shellSite'),
            $query = $view.find('#sitequery'),
            $reset = $view.find('#sitereset'),
            $add = $view.find('#siteadd'),
            $update = $view.find('#siteupdate'),
            $delete = $view.find('#sitedelete'),
            $export = $view.find('#siteexport'),
            $searchForm = $view.find('#shellSite-search-form'),
            sortObject = {field: 'openDate', type: null},
            tableIns;

        form.render();

        initTable();

        laydate.render({
            elem: '#openDate',
            range: true,
            trigger: 'click'
        });

        window.viewSiteInfo=function(siteid,siteName) {
        	febs.modal.open('查看'+ siteName +'信息', 'site/shell/siteView/' + siteid, {
                area: $(window).width() <= 750 ? '100%' : '100%',
                yes: function (index, layero) {
                    $('#shellSite-view').find('#oilInfosubmit').trigger('click');
                },
                btn2: function () {
                    $('#shellSite-view').find('#reset').trigger('click');
                    return false;
                }
            });
        }
        
        table.on('tool(shellSiteTable)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            console.log(data.siteid);
            if (layEvent === 'infoadd') {
            	febs.view.tab.change("/site/shell/info/" + data.siteid);
            }
            if (layEvent === 'delete') {
                febs.modal.confirm('删除油站', '确定删除该油站？', function () {
                	deleteShellSite(data.siteid);
                });
            }
            if (layEvent === 'update') {
                febs.modal.open('修改油站', 'site/shell/update/' + data.siteid, {
                    area: $(window).width() <= 750 ? '100%' : '100%',
                    btn: ['提交', '取消'],
                    yes: function (index, layero) {
                        $('#shellSite-update').find('#submit').trigger('click');
                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                });
            }
        });

        table.on('sort(shellSiteTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        $add.on('click', function () {
        	febs.modal.open('新增壳牌油站', 'site/shell/add', {
                btn: ['提交', '重置'],
                area: $(window).width() <= 750 ? '100%' : '100%',
                yes: function (index, layero) {
                    $('#shellSite-add').find('#submit').trigger('click');
                },
                btn2: function () {
                    $('#shellSite-add').find('#reset').trigger('click');
                    return false;
                }
            });
        });
        
        $export.on('click', function () {
        	var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            params.pageSize = $view.find(".layui-laypage-limits option:selected").val();
            params.pageNum = $view.find(".layui-laypage-em").next().html();
            febs.download(ctx + 'site/excel', params, '壳牌油站信息表.xlsx');
        });
        
        
        
        
        $delete.on('click', function () {
        	var checkStatus = table.checkStatus('shellSiteTable');
        	if (!checkStatus.data.length) {
                febs.alert.warn('请选择需要删除的油站');
            } else {
                febs.modal.confirm('删除油站', '确定删除该油站？', function () {
                    var siteIds = [];
                    layui.each(checkStatus.data, function (key, item) {
                    	siteIds.push(item.siteid)
                    });
                    deleteShellSite(siteIds.join(','));
                });
            }
        });
        
        treeSelect.render({
            elem: $view.find('#s-siteArea'),
            type: 'get',
            data: ctx + 'dictsite/parentTree/4',
            placeholder: '请选择',
            search: false
        });
        
        $query.on('click', function () {
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            treeSelect.revokeNode('s-siteArea');
            tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
        }); 

        function initTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'shellSiteTable',
                url: ctx + 'shell/list',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'siteCode', title: '油站编码', minWidth: 100,'align':'center'},
                    {field: 'siteName', title: '油站名称', minWidth: 250,'align':'center',templet:'#shellSite-titlink'},
                    {field: 'siteManagerName', title: '油站经理', minWidth: 100,'align':'center'},
                    {field: 'siteAreaName', title: '油站区域', minWidth: 100,'align':'center'},
                    {field: 'siteTmName', title: 'TM', minWidth: 100,'align':'center'},
                    {title: '操作', toolbar: '#shellSite-option', minWidth: 200,'align':'center'}
                ]]
            });
        }

        function getQueryParams() {
            var openDateFrom,
                openDateTo,
                openDate = $searchForm.find('input[name="s-openDate"]').val();
            if (openDate) {
                openDateFrom = openDate.split(' - ')[0];
                openDateTo = openDate.split(' - ')[1];
            }
            return {
                openDateFrom: openDateFrom,
                openDateTo: openDateTo,
                siteCode: $searchForm.find('input[name="s-siteCode"]').val().trim(),
                /* status: $searchForm.find("select[name='s-status']").val(), */
                siteName: $searchForm.find("input[name='s-siteName']").val(),
                siteArea: $searchForm.find("input[name='s-siteArea']").val().trim(),
                invalidate_ie_cache: new Date()
            };
        }

        function deleteShellSite(siteids) {
            febs.get(ctx + 'shell/delete/' + siteids, null, function () {
                febs.alert.success('删除油站成功');
                $query.click();
            });
        }
    })
</script>